/*
*	javascript for mserver
*
**/
//(function (){
	if(typeof String.prototype.trim !== 'function') {
	  String.prototype.trim = function() {
	    return this.replace(/^\s+|\s+$/g, ''); 
	  }
	}
	function onShowImage(fileName){
		var data = '<img src='+fileName +' ></img>';
		$('div#divDlg').html(data);
		$('div#divDlg').dialog({dialogClass:"alert"}) ;	
	}
	function onSwitchServer()
	{
		var mode = $('select#mode').val() ;
		try
		{
			$.ajax({
				type:"get",
				async:false,
				data:{
					'actionType':'switchServer',
					'mode':mode
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					if(data == '0')
						alert('切换模式成功');
					else
						alert('切换模式失败:'+getErrMsg(data));
				},
				error:function(){
					alert('切换模式失败') ;
				}
			});
		}catch(e){
			alert('切换模式失败:'+e) ;
			return false ;
		}
		return false ;				
	}
	function onChangeMode(){
		var key=$('input#key').val();
		var maxMsgLocal = $('input#maxMsgLocal').val();
		var transThreshold = $('input#transThreshold').val();
		var pointRatio0008 = $('input#pointRatio0008').val();
		var pointRatio0816 = $('input#pointRatio0816').val();
		var pointRatio1624 = $('input#pointRatio1624').val();
		try
		{
			$.ajax({
				type:"get",
				async:false,
				data:{
					'actionType':'changeMode',
					//'mode':mode,
					'maxMsgLocal':maxMsgLocal,
					'transThreshold':transThreshold,
					'pointRatio0008':pointRatio0008,
					'pointRatio0816':pointRatio0816,
					'pointRatio1624':pointRatio1624,
					'key':key
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					if(data == '0')
						alert('更新服务器设置成功');
					else
						alert('更新服务器设置失败:'+getErrMsg(data));
				},
				error:function(){
					alert('更新服务器设置失败') ;
				}
			});
		}catch(e){
			alert('更新服务器设置失败:'+e) ;
			return false ;
		}
		return false ;		
	}
	function newDate(dateStr){
		//var dateStr="2011-08-03 09:15:11"; 
		var a=dateStr.split(" ");
		var d=a[0].split("-");
		if (d.length != 3 || !d[0] || !d[1] || !d[2])
			return null;
		var t=a[1].split(":");
		if (t.length != 3 || !t[0] || !t[1] || !t[2])
			return null ;
		var date = new Date(d[0],(d[1]-1),d[2],t[0],t[1],t[2]);
		return date ;
	}
	function onStasticClient(){
		var clientName = $('input#clientName').val().trim();
		var minStasticTime = $('input#minStasticTime').val().trim();
		var maxStasticTime = $('input#maxStasticTime').val().trim();
		var pageNo = 1;
		if($("select#pageNo").length > 0)
			pageNo = $("select#pageNo").val();
		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'stasticClient',
					'clientName':clientName,
					'minStasticTime':minStasticTime,
					'maxStasticTime':maxStasticTime,
					'pageNo':pageNo
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					// clear previous mainDiv
					var preDlg = $("div#main>form"); 
					//preDlg.hide();
					$("div#main").empty();
					$("div#main").append(preDlg);
					$("div#main").append(data);
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;
	}
	function onQueryClient(){
		var clientName = $('input#clientName').val().trim();
		var pageNo = 1;
		if($("select#pageNo").length > 0)
			pageNo = $("select#pageNo").val();
		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'queryClient',
					'clientName':clientName,
					'pageNo':pageNo
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					// clear previous mainDiv
					var preDlg = $("div#main>form"); 
					//preDlg.hide();
					$("div#main").empty();
					$("div#main").append(preDlg);
					$("div#main").append(data);
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;
	}
	function freezeAccount(account){
		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'freezeAccount',
					'account':account
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					if('0' == data)
						showDialog('冻结子账号'+account+'成功');
					else
						showDialog('冻结子账号'+account+'失败:'+data);
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;	
	}
	function unfreezeAccount(account){
		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'unfreezeAccount',
					'account':account
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					if('0' == data)
						showDialog('解冻子账号'+account+'成功');
					else
						showDialog('解冻子账号'+account+'失败:'+data);
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;	
	}
	function freezeUser(username){
		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'freezeUser',
					'userName':username
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					if('0' == data)
						showDialog('冻结用户'+username+'成功');
					else
						showDialog('冻结用户'+username+'失败:'+data);
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;	
	}
	function unfreezeUser(username){
		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'unfreezeUser',
					'userName':username
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					if('0' == data)
						showDialog('解冻用户'+username+'成功');
					else
						showDialog('解冻用户'+username+'失败:'+data);
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;		
	}
	function onQueryAccount(){
		var userName = $("input#userName").val().trim() ;
		var account = $("input#account").val().trim() ;
		var min_validDate_year = $("select#min_validDate_year").val();
		var min_validDate_month = $("select#min_validDate_month").val();
		var min_validDate_day = $("select#min_validDate_day").val();
		var min_validDate = min_validDate_year + '-' + min_validDate_month + '-' + min_validDate_day;
		if ((min_validDate_year != '' || min_validDate_month != '' ||  min_validDate_day != '') && 
			min_validDate.length != 10)
		{
			alert('请输入完整的日期');
			return ;
		}  
		var max_validDate_year = $("select#max_validDate_year").val();
		var max_validDate_month = $("select#max_validDate_month").val();
		var max_validDate_day = $("select#max_validDate_day").val();
		var max_validDate = max_validDate_year + '-' + max_validDate_month + '-' + max_validDate_day;		
		if ((max_validDate_year != '' || max_validDate_month != '' ||  max_validDate_day != '') && 
			max_validDate.length != 10)
		{
			alert('请输入完整的日期');
			return ;
		}  
		
		var min_left_point = $("input#min_left_point").val().trim() ;
		var max_left_point = $("input#max_left_point").val().trim() ;
		
		var pageNo = '1';
		if($("select#pageNo").length == 1)
			pageNo=$("select#pageNo").val();
		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'queryAccount',
					'userName':userName,
					'account':account,
					'minValidDate':min_validDate,
					'maxValidDate':max_validDate,
					'minLeftPoint':min_left_point,
					'maxLeftPoint':max_left_point,
					'pageNo':pageNo
				},
				url:"admin",
				dataType:"html",
				success:function(data,textStatus){
					// clear previous mainDiv
					var preDlg = $("div#main>form"); 
					//preDlg.hide();
					$("div#main").empty();
					$("div#main").append(preDlg);
					$("div#main").append(data);
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;
	}	
	function onQueryLogger(){
		var pageNo = '1';
		var operation = $('select#operation').val();
		var operator = $('input#operator').val().trim();
		var operaA = $('input#operaA').val().trim() ;
		var operaB = $('input#operaB').val().trim() ;
		if($("select#pageNo").length ==1 )
			pageNo=$("select#pageNo").val();
		var pSendDate = $('input#rechargeDate').val().trim();
		var pMinSendTime = $('input#minRechargeTime').val().trim() ;
		var pMaxSendTime = $('input#maxRechargeTime').val().trim() ;
		var minRechargeTime = 0 ;
		var maxRechargeTime = 0 ;
		if(pSendDate != '' && pMinSendTime != '')
		{
			var date = newDate(pSendDate + ' ' + pMinSendTime);
			if (date)
				minRechargeTime = Date.parse(date);
			else
			{
				alert('请输入正确日期');
				return false;
			}
		}
		if(pSendDate != '' && pMaxSendTime != '')
		{
			var date = newDate(pSendDate + ' ' + pMaxSendTime);
			if (date)
				maxRechargeTime = Date.parse(date);
			else
			{
				alert('请输入正确日期');
				return false;
			}
		}

		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'queryLogger',
					'operation':operation,
					'operator':operator,
					'operaA':operaA,
					'operaB':operaB,
					'pageNo':pageNo,
					'minRechargeTime':minRechargeTime,
					'maxRechargeTime':maxRechargeTime
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					// clear previous mainDiv
					var preDlg = $("div#main>form"); 
					$("div#main").empty();
					$("div#main").append(preDlg);
					$("div#main").append(data);
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;			
	}
	function onQueryAnswerRecord(){
		var pageNo = '1';
		var account = $('input#account').val().trim();
		var clientName = $('input#clientName').val().trim();
		var userName = $('input#userName').val().trim() ;
		var gameType = $('input#gameType').val().trim() ;
		var ip = $('input#ip').val().trim() ;
		var status = $('select#status').val();
		var returnUser = $('input#returnUser').val().trim() ;
		var msgId = $('input#msgId').val().trim();
		if($("select#pageNo").length ==1 )
			pageNo=$("select#pageNo").val();
		var pSendDate = $('input#sendDate').val().trim();
		var pMinSendTime = $('input#minSendTime').val().trim() ;
		var pMaxSendTime = $('input#maxSendTime').val().trim() ;
		var minSendTime = 0 ;
		var maxSendTime = 0 ;
		if(pSendDate != '' && pMinSendTime != '')
		{
			//minSendTime = Date.parse(pSendDate + 'T' + pMinSendTime);
			var date = newDate(pSendDate + ' ' + pMinSendTime);
			if (date)
				minSendTime = Date.parse(date);
			else
			{
				alert('请输入正确日期');
				return false;
			}
		}
		if(pSendDate != '' && pMaxSendTime != '')
		{
			//maxSendTime = Date.parse(pSendDate + 'T' + pMaxSendTime);
			var date = newDate(pSendDate + ' ' + pMaxSendTime);
			if (date)
				maxSendTime = Date.parse(date);
			else
			{
				alert('请输入正确日期');
				return false;
			}
		}

		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'queryAnswerRecord',
					'account':account,
					'clientName':clientName,
					'userName':userName,
					'gameType':gameType,
					'ip':ip,
					'status':status,
					'returnUser':returnUser,
					'msgId':msgId,
					'pageNo':pageNo,
					'minSendTime':minSendTime,
					'maxSendTime':maxSendTime
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					// clear previous mainDiv
					var preDlg = $("div#main>form"); 
					//preDlg.hide();
					$("div#main").empty();
					$("div#main").append(preDlg);
					$("div#main").append(data);
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;		
	}
	function onUpdateClient(){
		var clientId = $('input#clientId').val();
		var password = $('input#password').val().trim();
		var passwordConfirm = $('input#passwordConfirm').val().trim();
		var name = $('input#name').val().trim() ;
		var phone= $('input#phone').val().trim();
		if (!password || !passwordConfirm)
		{
			alert('输入项不能为空');
			return false ;
		} 
		if (password != passwordConfirm)
		{
			alert('密码不匹配') ;
			return false ;
		}
		// send ajax request
		$.ajax({
				type: "get",
				async:false,
				data:{
					'actionType':'updateClient',
					'clientId':clientId,
					'password':password,
					'name':encodeURI(name),
					'phone':phone
				},
				dataType:"text",
				cache:false,
				url: 'admin',
				success: function(data, textStatus){
					if (data == '0')
					{
						alert('更新客户端成功:'+clientId);
						closeDialog();
					}
					else
					{
						alert('更新客户端失败:'+getErrMsg(data));
					}
					return false ;
					
				}
			});	
		return false ;		
	}
	function onUpdateGameType(){

		try{
			var gameType = $("input#gameType").val();
			var gameDescription = $("input#gameDiscription").val().trim();
			gameDescription = encodeURI(gameDescription);
			var answerType = $("select#answerType").val();
			var answerLen = $("input#answerLen").val();
			var point = $("input#point").val().trim();
			var pointWorker = $("input#pointWorker").val().trim();
			var interval = $("input#interval").val().trim();
			var transOn = $('select#transOn').val().trim() ;
			var difficulty = $('input#difficulty').val().trim() ;

			if (gameDescription=='' || answerType == '' || answerLen == '' ||
					point == '' || pointWorker == '' ||  interval == '' || difficulty == '')
			{
				alert('输入项不能为空') ;
				return false ;
			}
			
			// send ajax request
			$.ajax({
				type: "get",
				async:false,
				data:{
					'actionType':'updateGameType',
					'gameType':gameType,
					'gameDescription':gameDescription,
					'answerType':answerType,
					'answerLen':answerLen,
					'point':point,
					'pointWorker':pointWorker,
					'interval':interval,
					'transOn':transOn,
					'difficulty':difficulty
				},
				dataType:"text",
				cache:false,
				url: 'admin',
				success: function(data, textStatus){
					if (data == '0')
					{
						alert('更新项目类型成功:'+gameType);
						closeDialog();
					}
					else
					{
						alert('更新项目类型失败:'+getErrMsg(data));
					}
					return false ;
					
				}
			});	
		}
		catch(e){
			alert(e);
		}
		return false ;		
	}
	function onEditUser(userId,returnRatio){
		var content = '<div><p>';
		
		content += '<input id=userId type=hidden value='+userId+'></input>' ;
		content += '<input id=returnRatio type=number value='+returnRatio+'></input>';
		content += '</p><p>';
		content += '<input type=button onclick="updateUser(); return false" value=更新></input>';
		content +='</p></div>' ;
		showDialog(content,'修改返点率');
	}
	function updateUser()
	{
		try
		{
			var userId = $('input#userId').val().trim() ;
			var returnRatio = $('input#returnRatio').val().trim() ;
			$.ajax({
				type:"get",
				async:false,
				data:{
					'actionType':'updateUser',
					'userId':userId,
					'returnRatio':returnRatio
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					if (data == '0')
						alert('更新成功');
					else
						alert('更新失败：'+data);
				}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;			
	}
	function onEditClient(clientId){
		try
		{
			$.ajax({
				type:"get",
				async:false,
				data:{
					'actionType':'querySingleClient',
					'clientId':clientId
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					showDialog(data,'更新客户端');
				}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;				
		
	}	
	function onEditGameType(gameType){
		try
		{
			$.ajax({
				type:"get",
				async:false,
				data:{
					'actionType':'querySingleGameType',
					'gameType':gameType
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					showDialog(data,'更新项目类型');
				}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;				
		
	}
	function onDeleteGameType(gameType){
		if(!confirm("确定删除项目类型:"+gameType))
			return false ;
		try
		{
			$.ajax({
				type:"get",
				async:false,
				data:{
					'actionType':'deleteGameType',
					'gameType':gameType
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					if(data == '0')
					{
						$("#"+gameType).empty();
					}
					else
						alert('删除失败:'+getErrMsg(data));
				}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;		
	}
	function onCreateClient(){
		try{
			var clientName = $("input#ClientName").val().trim();
			var password = $("input#password").val().trim();
			var passwordConfirm = $("input#passwordConfirm").val().trim();
			var name = $('input#name').val().trim() ;
			var phone= $('input#phone').val().trim();

			if (clientName=='' || password == '' || passwordConfirm == '')
			{
				alert('输入项不能为空') ;
				return false ;
			}
			if (password != passwordConfirm)
			{
				alert('密码不匹配') ;
				return false ;
			}
			
			// send ajax request
			$.ajax({
				type: "get",
				async:false,
				data:{
					'actionType':'createClient',
					'clientName':clientName,
					'password':password,
					'name':encodeURI(name),
					'phone':phone
				},
				dataType:"text",
				cache:false,
				url: 'admin',
				success: function(data, textStatus){
					if (data.charAt(0) >= '1' && data.charAt(1) <= '9')
					{
						alert('创建客户端成功:'+data);
						closeDialog();
					}
					else
					{
						alert('创建客户端失败:'+getErrMsg(data));
					}
					return false ;
					
				}
			});	
		}
		catch(e){
			alert(e);
		}
		return false ;
	}
	function onDeleteUser(userId){
		
		if(!confirm("确定删除用户:"+userId))
			return false ;
		else
		{
			alert('暂未实现');
			return false ;
		}
		try
		{
			$.ajax({
				type:"get",
				async:false,
				data:{
					'actionType':'deleteUser',
					'userId':userId
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					if(data == '0')
					{
						$("#"+userId).empty();
					}
					else
						alert('删除失败:'+getErrMsg(data));
				}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;		
	}
	function onDeleteClient(clientId){
		if(!confirm("确定删除客户端:"+clientId))
			return false ;
		try
		{
			$.ajax({
				type:"get",
				async:false,
				data:{
					'actionType':'deleteClient',
					'clientId':clientId
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					if(data == '0')
					{
						$("#"+clientId).empty();
					}
					else
						alert('删除失败:'+getErrMsg(data));
				}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;		
	}
	function onQueryUser(){
		var userName = $('input#userName').val().trim();
		var pageNo = 1;
		if($("select#pageNo").length > 0)
			pageNo = $("select#pageNo").val();
		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'queryUser',
					'userName':userName,
					'pageNo':pageNo
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					// clear previous mainDiv
					var preDlg = $("div#main>form"); 
					//preDlg.hide();
					$("div#main").empty();
					$("div#main").append(preDlg);
					$("div#main").append(data);
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;		
	}
	function onQueryGameType(){
		var pageNo = 1;
		if($("select#pageNo").length > 0)
			pageNo = $("select#pageNo").val();
		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'queryGameType',
					'pageNo':pageNo
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					$("div#main").html(data);
				}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;	
	}
	function openCreateClientDlg(){
		try
		{
			$.ajax({
				type:"get",
				async:false,
				data:{
					'actionType':'getCreateClientDlg'
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					showDialog(data,'新建客户端');
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}	
	}
	function openCreateGameTypeDlg(){
		try
		{
			$.ajax({
				type:"get",
				async:false,
				data:{
					'actionType':'getCreateGameTypeDlg'
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					showDialog(data,'新建项目类型');
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}	
	}
	function querySingleAccount(account){
		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'querySingleAccount',
					'account':account
				},
				url:"admin",
				dataType:"text",
				success:function(data,textStatus){
					showDialog(data,'子帐号明细');
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;
	}	
	function showDialog(data,tt){
		$('div#divDlg').html(data);
		$('div#divDlg').dialog({dialogClass:"alert",title:tt}) ;		
	}	
	function closeDialog(){
		
		$('div#divDlg').dialog("close");
	}
	
	function onCreateGameType(){
		try{
			var gameDescription = $("input#gameDiscription").val().trim();
			gameDescription = encodeURI(gameDescription);
			var answerType = $("select#answerType").val();
			var answerLen = $("input#answerLen").val();
			var point = $("input#point").val().trim();
			var pointWorker = $("input#pointWorker").val().trim();
			var interval = $("input#interval").val().trim();
			var transOn = $("select#transOn").val().trim();
			//var difficulty = $("input#difficulty").val().trim() ;
			var difficulty = 1.0 ;
			if (gameDescription=='' || answerType == '' || answerLen == '' ||
					point == '' || pointWorker == '' ||  interval == '')
			{
				alert('输入项不能为空') ;
				return false ;
			}
			
			// send ajax request
			$.ajax({
				type: "get",
				async:false,
				data:{
					'actionType':'createGameType',
					'gameDescription':gameDescription,
					'answerType':answerType,
					'answerLen':answerLen,
					'point':point,
					'pointWorker':pointWorker,
					'interval':interval,
					'transOn':transOn,
					'difficulty':difficulty
				},
				dataType:"text",
				cache:false,
				url: 'admin',
				success: function(data, textStatus){
					if (data.charAt(0) == '1')
					{
						alert('创建项目类型成功:'+data);
						closeDialog();
					}
					else
					{
						alert('创建项目类型失败:'+getErrMsg(data));
					}
					return false ;
					
				}
			});	
		}
		catch(e){
			alert(e);
		}
		return false ;
	}	
	function onClickSideBar(element){
		try
		{
			// update side bar status
			$('a[name="sideNavBar"]').each(function(i,ele){jQuery.attr(ele,'class','');});					
			jQuery.attr(element,'class','active');
						
			// send ajax request
			$.ajax({
				type: "get",
			//	async:false,
				cache:false,
				url: element.href,
				dataType:"text",
				beforeSend: function(XMLHttpRequest){
				//ShowLoading();
				},
				success: function(data, textStatus){
					// clear main div
					//if(element.type != 'submit')
					$("div#main").empty();
					// clear previous mainDiv
					$("div#main").append(data);
					
					return false ;
				},
				complete: function(XMLHttpRequest, textStatus){
				//HideLoading();
				},
				error: function (XMLHttpRequest, textStatus, errorThrown){ 
					alert(textStatus+':'+XMLHttpRequest.reponseText);
	                return false ;
	            }
			});	
		}
		catch(e){
			alert(e);
		}
		
		return false ;
	}
	function onClickMainNavBar(element){
		try
		{
			// send ajax request
			$.ajax({
				type: "get",
			//	async:false,
				dataType: "json",
				cache:false,
				url: element.href,
				beforeSend: function(XMLHttpRequest){
				//ShowLoading();
				},
				success: function(data, textStatus){
					// update main navigater
					$('a[name="mainNavBar"]').each(function(i,ele){jQuery.attr(ele,'class','');});					
					jQuery.attr(element,'class','active');
					
					// update sider bar				
					var sideNav = $("ul.sideNav");
					sideNav.empty();	
					$.each(data,function(i,item){
						var siderBar = '<li><a href="'+item.url+ '" id="' + item.id +
									'" name="sideNavBar" onclick="onClickSideBar(this);return false;">'+
									item.title+'</a></li>';
						sideNav.append(siderBar);
					});
				//	alert(4);
					
					// clear previous mainDiv
					$('div[name="contentDiv"]').each(function(i,ele){
						ele.style.display='none';
						//ele.hide("slide");
					});
					$("div#main").html("");
					$("div#main").show();
					return false ;
				},
				complete: function(XMLHttpRequest, textStatus){
				//HideLoading();
					return false ;
				},
				error: function (XMLHttpRequest, textStatus, errorThrown){ 
					alert(XMLHttpRequest.responseText+':'+textStatus);
					return false ;
	            }
			});	
		}
		catch(e){
			alert(e);
		}
		// 防止触发页面跳转
		return false ;
	}
	function onCreateRechargeCard(){
		try{
			var userName = $("input#userName").val().trim();
			var password = $("input#password").val().trim();
			var point = $("input#point").val();
			var batchSize = $("input#batchSize").val();
			var year = $("select#validDate_year").val();
			var month = $("select#validDate_month").val();
			var day = $("select#validDate_day").val();
			var validDate = year+'-'+month+'-'+day;
			
			if (userName=='' || password == '' || point == '' ||
					batchSize == '' || year == '' || month == '' || day == '')
			{
				alert('输入项不能为空') ;
				return false ;
			}
			
			// send ajax request
			$.ajax({
				type: "get",
				async:false,
				data:{
					'actionType':'createRechargeCard',
					'userName':userName,
					'password':password,
					'point':point,
					'batchSize':batchSize,
					'validDate':validDate
				},
				dataType:"text",
				cache:false,
				url: 'user',
				beforeSend: function(XMLHttpRequest){
				//ShowLoading();
				},
				success: function(data, textStatus){
					if (data.length <32)
					{
						alert('创建充值卡失败：'+getErrMsg(data));
					}
					else
					{
						var accountList = data.split(',');
						var accounts = '共创建'+accountList.length+'张充值卡：\n';
						for(var i = 0 ; i < accountList.length ; i++)
							accounts += accountList[i] + '\t\t\n';
						//alert(accounts);
						showDialog(accounts,"充值卡");
					}
					return false ;
					
				},
				complete: function(XMLHttpRequest, textStatus){
				//HideLoading();
				},
				error: function (XMLHttpRequest, textStatus, errorThrown){ 
	                alert('创建充值卡失败：'+XMLHttpRequest.responseText+textStatus);
	                return false ;
	            }
			});	
		}
		catch(e){
			alert(e);
		}
		return false ;
	}	
	function onQueryRechargeCard(){
		var rechargeCard = $("input#rechargeCard").val().trim() ;
		var min_validDate_year = $("select#min_validDate_year").val();
		var min_validDate_month = $("select#min_validDate_month").val();
		var min_validDate_day = $("select#min_validDate_day").val();
		var min_validDate = min_validDate_year + '-' + min_validDate_month + '-' + min_validDate_day;
		
		var max_validDate_year = $("select#max_validDate_year").val();
		var max_validDate_month = $("select#max_validDate_month").val();
		var max_validDate_day = $("select#max_validDate_day").val();
		var max_validDate = max_validDate_year + '-' + max_validDate_month + '-' + max_validDate_day;		
	
		var min_point = $("input#min_point").val().trim() ;
		var max_point = $("input#max_point").val().trim() ;
		
		var pageNo = '1';
		if($("select#pageNo").length == 1)
			pageNo=$("select#pageNo").val();
		try
		{
			$.ajax({
				type:"get",
				async:false,
				cache:false,
				data:{
					'actionType':'queryRechargeCard',
					'rechargeCard':rechargeCard,
					'minValidDate':min_validDate,
					'maxValidDate':max_validDate,
					'minPoint':min_point,
					'maxPoint':max_point,
					'pageNo':pageNo
				},
				url:"user",
				dataType:"html",
				success:function(data,textStatus){
					// clear previous mainDiv
					var preDlg = $("div#main>form"); 
					//preDlg.hide();
					$("div#main").empty();
					$("div#main").append(preDlg);
					$("div#main").append(data);
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;
	}
	function recycleRechargeCard(rechargeCard){
	
		if(!confirm("确定要删除充值卡:"+rechargeCard))
			return false ;
		try
		{
			$.ajax({
				type:"get",
				async:false,
				data:{
					'actionType':'recycleRechargeCard',
					'rechargeCard':rechargeCard
				},
				url:"user",
				dataType:"text",
				success:function(data,textStatus){
					if(data == '0')
					{
						var eId = rechargeCard.replace('|','\\|');
						$("#"+eId).empty();
					//	alert('删除成功:'+rechargeCard);
					}
					else
						alert('删除失败:'+getErrMsg(data));
				},
				error:function(){}
			});
		}catch(e){
			alert(e);
			return false ;
		}
		return false ;
	}	
//}());